package yxy.game.pm2.module.godwar;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import org.redisson.api.RList;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.module.battle.FighterModule;

public class GodWarRecord extends AreaModule {
	// bean===========================================================================
	private int dataId;
	private int evolve;

	private String playerCode;
	private String nickName;
	private long power;
	private ArrayList<FighterModule> fighters = new ArrayList<>();

	private DateTime date;// 时间
	private String recordCode;// 录像code

	public int getDataId() {
		return dataId;
	}

	public void setDataId(int dataId) {
		this.dataId = dataId;
	}

	public int getEvolve() {
		return evolve;
	}

	public void setEvolve(int evolve) {
		this.evolve = evolve;
	}

	public String getPlayerCode() {
		return playerCode;
	}

	public void setPlayerCode(String playerCode) {
		this.playerCode = playerCode;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public long getPower() {
		return power;
	}

	public void setPower(long power) {
		this.power = power;
	}

	public ArrayList<FighterModule> getFighters() {
		return fighters;
	}

	public void setFighters(ArrayList<FighterModule> fighters) {
		this.fighters = fighters;
	}

	public DateTime getDate() {
		return date;
	}

	public void setDate(DateTime date) {
		this.date = date;
	}

	public String getRecordCode() {
		return recordCode;
	}

	public void setRecordCode(String recordCode) {
		this.recordCode = recordCode;
	}

	// func===========================================================================
	// redis==========================================================================

	static public String key(int areaId, int dataId) {
		String moduleName = String.format("GODWARRECORD:%s", dataId);
		return key(areaId, moduleName);
	}

	static public String key(String playerCode, int dataId) {
		AreaBean areaBean = area(playerCode);
		String moduleName = String.format("GODWARRECORD:%s", dataId);
		return key(areaBean.getAreaId(), moduleName);
	}

	static private RList<GodWarRecord> touch(String playerCode, int dataId) throws RedisException {
		return RedisManager.client(redis()).getList(key(playerCode, dataId));
	}

	static public List<GodWarRecord> get(String playerCode, int dataId) {
		try {
			List<GodWarRecord> list = touch(playerCode, dataId).readAll();
			return list;
		} catch (RedisException e) {
			e.printStackTrace();
			return new ArrayList<>();
		}
	}

	static public void add(String playerCode, GodWarRecord record) {
		try {
			RList<GodWarRecord> rlist = touch(playerCode, record.getDataId());
			rlist.add(0, record);
			if (rlist.size() > 20) {
				rlist.trim(0, 19);
			}
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getAreaId(), getDataId()));
	}
}
